<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-header">
        <h2>欢迎登录</h2>
      </div>
      <el-form :model="loginForm" @submit.native.prevent="handleLogin" class="login-form">
        <el-form-item>
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from "vuex";

export default {
  name: "LoginView",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    ...mapActions("user", ["login"]), // 显式声明使用 user 模块中的 login 方法
    handleLogin() {
      const { username, password } = this.loginForm;
      if (!username || !password) {
        this.$message.error("请输入用户名和密码");
        return;
      }
      this.login({ username, password })
        .then(() => {
          this.$message.success("登录成功");
          this.$router.push("/AccountManagementView"); // 登录成功后跳转
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
  },
};
</script>

<style scoped>
/* 背景样式 */
.login-container {
  margin-left: -5px;
  margin-top: -5px;
  width: 98vw;
  height: 96vh;
  background: url('@/assets/background.jpg') no-repeat center center;
  background-size: cover; /* 确保背景图片覆盖整个容器 */
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden; /* 防止内容溢出导致滚动条 */
}

/* 登录框样式 */
.login-wrapper {
  background: rgba(255, 255, 255, 0.9);
  padding: 40px 60px;
  border-radius: 8px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  width: 400px;
  max-width: 90%;
  text-align: center;
  overflow: hidden; /* 防止内容溢出 */
}

/* 头部样式 */
.login-header {
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  margin-bottom: 10px;
}

.login-header h2 {
  font-size: 24px;
  color: #333;
}

/* 输入框样式 */
.login-form .el-form-item {
  margin-bottom: 20px;
}

.el-input {
  border-radius: 4px;
}

/* 按钮样式 */
.login-button {
  width: 100%;
  height: 40px;
  border-radius: 4px;
  background-color: #409EFF;
  border-color: #409EFF;
  transition: background-color 0.3s, border-color 0.3s;
}

.login-button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-wrapper {
    padding: 30px 20px;
  }

  .login-header h2 {
    font-size: 20px;
  }

  .logo {
    width: 60px;
  }
}
</style>
